//
//  NNCalendarPageView.m
//  TestObjectProduct
//
//  Created by fangshufeng on 16/9/30.
//  Copyright © 2016年 fangshufeng. All rights reserved.
//

#import "NNCalendarPageView.h"
#import "NNCalendarDayCell.h"
#import "NNCalendarModel.h"
#import "NNCalendarScroViewConfigs.h"
#import "NNCalendarPageHeadView.h"
#import "NNCalendarPageHeadDemoView.h"

@interface NNCalendarPageView ()
<
UICollectionViewDelegate,
UICollectionViewDataSource
>

@property(nonatomic,strong) UICollectionView *collectionView;
/***/
@property(nonatomic, strong) NNCalendarPageHeadView *headView;
@property(nonatomic, strong) NNCalendarPageHeadDemoView *headView1;

/**通过xib加载cell nibWithNibName*/
@property(nonatomic, copy) NSString *nibWithNibNameStr;
/**通过代码加载cell registerClass*/
@property(nonatomic, copy) NSString *registerClassName;

/**重用标识CellWithReuseIdentifier*/
@property(nonatomic, copy) NSString *cellWithReuseIdentifier;

@end

@implementation NNCalendarPageView

-(instancetype)initWithNibNameStr:(NSString *)nibWithNibNameStr cellWithReuseIdentifier:(NSString *)cellWithReuseIdentifier calendarPageViewBeginDay:(NNCalendarPageViewBeginDay)calendarPageViewBeginDay{
    if (self = [super init]) {
        _calendarPageViewBeginDay = calendarPageViewBeginDay;
        self.nibWithNibNameStr = nibWithNibNameStr;
        self.cellWithReuseIdentifier  = cellWithReuseIdentifier;
        [self configSubViews];
    }
    return self;
}
- (instancetype)initWithRegisterClassName:(NSString *)className cellWithReuseIdentifier:(NSString *)cellWithReuseIdentifier calendarPageViewBeginDay:(NNCalendarPageViewBeginDay)calendarPageViewBeginDay{
    if (self = [super init]) {
        _calendarPageViewBeginDay = calendarPageViewBeginDay;
        self.registerClassName = className;
        self.cellWithReuseIdentifier  = cellWithReuseIdentifier;
        [self configSubViews];
    }
    return self;
}

- (void)configSubViews {
    if (self.calendarPageViewBeginDay == NNNCalendarPageViewSunday) {
        [self addSubview:self.headView];
    }else {
        [self addSubview:self.headView1];
    }
    [self addSubview:self.collectionView];
}
- (void)layoutSubviews {
    [super layoutSubviews];
    if (self.calendarPageViewBeginDay == NNNCalendarPageViewSunday) {
        self.headView.frame = CGRectMake(0, 0, self.frame.size.width, KNNCalendarPageViewHeadViewHeight);
    }else {
        self.headView1.frame = CGRectMake(0, 0, self.frame.size.width, KNNCalendarPageViewHeadViewHeight);
    }
    self.collectionView.frame = CGRectMake(0, KNNCalendarPageViewHeadViewHeight, self.frame.size.width, self.frame.size.height - KNNCalendarPageViewHeadViewHeight);
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.pageItems.count;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat width = [self itemHeightWithWidth:self.bounds.size.width];
    return CGSizeMake(width, width);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    NNCalendarDayCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:self.cellWithReuseIdentifier forIndexPath:indexPath];

    if ([cell isKindOfClass:[NNCalendarDayCell class]]) {
        cell.calendarModel = self.pageItems[indexPath.row];
    }else {
        NSAssert([cell isKindOfClass:[NNCalendarDayCell class]], [NSString stringWithFormat:@"this cell is not NNCalendarDayCell subviews"]);
    }
    
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NNCalendarModel *model = self.pageItems[indexPath.row];
    if ([self.delegate respondsToSelector:@selector(calendarPageView:itemDidClick:)]) {
        [self.delegate calendarPageView:self itemDidClick:model];
    }
}
- (void)setPageItems:(NSArray *)pageItems {
    _pageItems = pageItems;
    [self.collectionView reloadData];
}
- (void)setHeadViewBackColor:(UIColor *)headViewBackColor {
    _headViewBackColor = headViewBackColor;
    self.headView.backgroundColor = headViewBackColor;
}
- (CGFloat)itemHeightWithWidth:(CGFloat)width {
    CGFloat backWidth = (width - (KNNCalendarPageViewColumn - 1) * KNNCalendarPageViewVerticalMargin - 2 * KNNCalendarPageViewSectionMargin) / KNNCalendarPageViewColumn;
    return backWidth;
}
#pragma mark - loazy load
- (UICollectionView *)collectionView {
    if (_collectionView == nil) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.minimumInteritemSpacing = KNNCalendarPageViewVerticalMargin;
        layout.minimumLineSpacing = KNNCalendarPageViewHorizontalMargin;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.contentInset = UIEdgeInsetsMake(KNNCalendarPageViewSectionMargin, KNNCalendarPageViewSectionMargin, KNNCalendarPageViewSectionMargin, KNNCalendarPageViewSectionMargin);
        if (self.nibWithNibNameStr) {
            [_collectionView registerNib:[UINib nibWithNibName:self.nibWithNibNameStr bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:self.cellWithReuseIdentifier];
            
        }else {
            [_collectionView registerClass:[NSClassFromString(self.registerClassName) class] forCellWithReuseIdentifier:self.cellWithReuseIdentifier];
        }
        _collectionView.delegate = self;
        
        _collectionView.dataSource = self;
        
        _collectionView.backgroundColor =  [UIColor clearColor];
    }
    return _collectionView;
}

- (NNCalendarPageHeadView *)headView {
    if (_headView == nil) {
        _headView = [NNCalendarPageHeadView calendarPageHeadView];
    }
    return _headView;
}
- (NNCalendarPageHeadDemoView *)headView1 {
    if (_headView1 == nil) {
        _headView1 = [NNCalendarPageHeadDemoView calendarPageHeadDemoView];
    }
    return _headView1;
}
@end
